//添加管理员
exports.add_master = function (req, res) {

    var bodyParams = req.routeInfo.bodyParams;
    //最终结果
    var resultData = {};

    var userInfo = 0;
    // var errCode = 0;
    //任务流创建
    async.waterfall([
        //1.查询要添加的用户是否已注册
        function (cb) {
            var mobile = bodyParams.mobile;
            var account = bodyParams.account;
            if (account.toString().length < 6) {
                errCode = {
                    errcode: "4",
                    errMsg: "账户长度应大于六位"
                };
                return RES.response(res, false, errCode);
            }
            if (mobile.toString().length < 6) {
                errCode = {
                    errcode: "3",
                    errMsg: "请输入正确的电话号码"
                };
                return RES.response(res, false, errCode);
            } else {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_user_info + "/u_username/" + bodyParams.account,
                        method: 'POST',
                        data: {
                            fields: ["u_id", "u_oid"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        userInfo = data.data;
                        cb();
                    }
                );
            }
        },
        function (cb) {
            if (userInfo.length !== 0 && userInfo[0].u_oid != 0) {
                errCode = {
                    errcode: "2",
                    errMsg: "添加失败，该用户已是管理员"
                };
                return RES.response(res, false, errCode);
            } else if (userInfo.length !== 0 && userInfo[0].u_oid == 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.updateUser,
                        method: 'POST',
                        data: {
                            u_username: bodyParams.account,
                            u_oid: bodyParams.organization_id,
                            u_type:1
                        }
                    },
                    function (err, data) {
                        console.log(data);
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.addOrganizationUser,
                                method: 'POST',
                                data: {
                                    ou_oid: bodyParams.organization_id,
                                    ou_uid: userInfo[0].u_id,
                                    ou_phone: bodyParams.mobile,
                                    ou_role: "机构管理员",
                                    ou_name: bodyParams.realname
                                }
                            },
                            function (err, data) {
                                console.log(data);
                                cb();
                            }
                        );
                    }
                );
            } else {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userAdd,
                        method: 'POST',
                        data: {
                            u_username: bodyParams.account,
                            u_realname: bodyParams.realname,
                            u_password: 123456,
                            u_phone: bodyParams.mobile,
                            u_type: 1,
                            u_oid: bodyParams.organization_id,
                            u_nickname: "机构管理员"
                        }
                    },
                    function (err, data) {

                        var u_uid = data;

                        /*resultData.addData = u_uid;
                        cb();*/
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.addOrganizationUser,
                                method: 'POST',
                                data: {
                                    ou_oid: bodyParams.organization_id,
                                    ou_uid: u_uid,
                                    ou_phone: bodyParams.mobile,
                                    ou_role: "机构管理员",
                                    ou_name: bodyParams.realname
                                }
                            },
                            function (err, data) {
                                console.log(data);

                                cb();
                            }
                        );
                    }
                );
            }
        }
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}